<?php

namespace App\Filament\Admin\Resources;

use App\Filament\Admin\Resources\ComponentTypeResource\Pages;
use App\Filament\Admin\Resources\ComponentTypeResource\RelationManagers;
use App\Models\ComponentType;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Forms\Components\Select;
use App\Models\Component as ComponentModel;
use Dotswan\FilamentCodeEditor\Fields\CodeEditor;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Database\Eloquent\Model;

class ComponentTypeResource extends Resource
{
    protected static ?string $model = ComponentType::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    protected static ?string $modelLabel = '组件类型';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Select::make('component_id')->searchable()
                    ->label('选择组件')
                    ->options(ComponentModel::all()->pluck('chinese_title', 'id'))
                    ->required(),
                Select::make('framework')->options([
                    'vue' => 'vue',
                    'react' => 'react',
                    'next' => 'next',
                    'nuxt' => 'nuxt',
                    'blade' => 'blade',
                ])->required()->default('vue'),
                CodeEditor::make('code')
                    ->label('代码')
                    ->darkModeTheme('gruvbox-dark')
                    ->lightModeTheme('basic-dark'),
                CodeEditor::make('demo_data')
                    ->label('样本数据')
                    ->darkModeTheme('gruvbox-dark')
                    ->lightModeTheme('basic-dark'),
                CodeEditor::make('component_tree')
                    ->label('组件结构')
                    ->darkModeTheme('gruvbox-dark')
                    ->lightModeTheme('basic-dark'),
                Repeater::make('dependencies')
                    ->label('NPM依赖包')
                    ->schema([
                    TextInput::make('name'),
                    TextInput::make('version'),
                ])->columns(2),
                Repeater::make('params')
                    ->label('参数')
                    ->schema([
                    TextInput::make('name'),
                    TextInput::make('default'),
                ])->columns(2)

            ])->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('component_id')->label('组件')->getStateUsing(function ($record) {
                    return ComponentModel::find($record->component_id)->title;
                }),
                TextColumn::make('framework')->label('框架'),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListComponentTypes::route('/'),
            'create' => Pages\CreateComponentType::route('/create'),
            'edit' => Pages\EditComponentType::route('/{record}/edit'),
        ];
    }
}
